package com.smart_campus.bus.controller;

import com.github.pagehelper.PageHelper;
import com.smart_campus.bus.domain.Building;
import com.smart_campus.bus.service.BuildingService;
import com.smart_campus.bus.vo.BuildingVo;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alfred
 * @time: 2020/6/20 10:32
 */
@RestController
@RequestMapping("building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 查询全部
     * @param buildingVo
     * @return
     */
    @RequestMapping("queryBuilding")
    public DataGridView queryBuilding(BuildingVo buildingVo){
        return buildingService.queryBuilding(buildingVo);
    }

    /**
     * @param building
     * 添加
     * @return
     */
    @RequestMapping("addBuilding")
    public ResultObj addBuilding(Building building){
        try {
            this.buildingService.insertSelective(building);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     * @param building
     * @return
     */
    @RequestMapping("updateBuilding")
    public ResultObj updateBuilding(Building building){
        try {
            this.buildingService.updateByPrimaryKeySelective(building);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delBuilding")
    public ResultObj delBuilding(Integer id){
        try {
            this.buildingService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("DeleteBatchBuilding")
    public ResultObj DeleteBatchBuilding(Integer[] ids){
        try {
            for (Integer id: ids) {
                this.buildingService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("selallBuilding")
    public List selallBuilding(){
        List<Building> list = buildingService.selall();
        return list;
    }
}
